package expression.v1.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import expression.v1.model.ExpImage;

@Repository(value = "expImgDaoImp")
public class ExpImageDaoImp extends BaseDao<ExpImage> implements IExpImageDao {

	@Override
	public int addImage(ExpImage expImage) {
		String sql = "insert into exp_pkgimgs(id,pkg,name,path,createtime) values(?,?,?,?,?)";
		return this.getJdbcTemplate().update(sql, expImage.getId(),
				expImage.getPkg(), expImage.getName(), expImage.getPath(),
				new Date());
	}

	@Override
	public int addImages(List<ExpImage> imgs) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int deleteImage(String id) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int deleteImages(List<String> ids) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<ExpImage> queryImgsByPkgId(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<ExpImage> queryImgsByPkgKeyword(String keyword, int from,
			int limit) {
		String sql = "select * from exp_pkgimgs where pkg=? limit ?,?";
		List<ExpImage> imgs = this.getJdbcTemplate().query(sql,
				new Object[] { keyword, from, limit }, new RowMapper<ExpImage>() {

					@Override
					public ExpImage mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						ExpImage img = new ExpImage();
						img.setId(rs.getString("id"));
						img.setName(rs.getString("name"));
						img.setPath(rs.getString("path"));
						img.setPkg(rs.getString("pkg"));
						return img;
					}
				});
		return imgs;
	}
	
	public int countAll(){
		String sql = "select count(*) from exp_pkgimgs";
		return this.getJdbcTemplate().queryForObject(sql, Integer.class);
	}
	
	@Override
	public int countByConditions(Object...args){
		String sql = "select count(*) from exp_pkgimgs where pkg=?";
		return this.getJdbcTemplate().queryForObject(sql, Integer.class,args);
	}
}
